﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class BaoCaoSoQuyNganHang : System.Web.UI.Page
{
    SqlParameter[] p;
    clsXuLy ex = new clsXuLy();
    string frmName = "frmsoquinganhang";
    static int f_SuDung = 0, f_Them = 0, f_Sua = 0, f_Xoa = 0, f_XemTatCa = 0, f_InBaoCao = 0;
    public string Header = System.Configuration.ConfigurationSettings.AppSettings["Header"].ToString();
    static System.Data.DataTable dt;
    static int PageNumber = 5;
    static int CurrentPage = 1;
    double tongphaitravat;
    double tongphaitrakhongvat;
    int pc;
    int loaicongno;
    float sotien;
    static DataTable dtPrint;
    NpoiExport cl = new NpoiExport();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(Convert.ToString(Session["user"])))
            gstGetMess("Hết phiên làm việc, vui lòng đăng nhập để tiếp tục", "Default.aspx");
        else
        {
            if (!IsPostBack)
            {

                string idnguoidung = Session["IDUser"].ToString();
                string sql = "select SuDung,Them,Sua,Xoa,InBaoCao,XemTatCa from XDAPhanQuyen where IDnguoidung='" + idnguoidung + "' and tag='" + frmName + "'";

                System.Data.DataTable dt2 = ex.GetData_Text(sql);
                if (dt2.Rows.Count > 0)
                {
                    f_SuDung = int.Parse(dt2.Rows[0]["SuDung"].ToString());
                    f_Them = int.Parse(dt2.Rows[0]["Them"].ToString());
                    f_Sua = int.Parse(dt2.Rows[0]["Sua"].ToString());
                    f_Xoa = int.Parse(dt2.Rows[0]["Xoa"].ToString());
                    f_InBaoCao = int.Parse(dt2.Rows[0]["InBaoCao"].ToString());
                    f_XemTatCa = int.Parse(dt2.Rows[0]["XemTatCa"].ToString());
                }
                if (f_SuDung.Equals(1))
                {
                    Title = "Báo cáo chi tiết tài khoản | " + Header;

                    btnCancel_Click(sender, e);
                    for (int i = 1; i <= 12; i++)
                    {
                        dlThang.Items.Add(new ListItem("Tháng " + i.ToString(), i.ToString()));
                    }
                    dlThang.Items.Insert(0, new ListItem("Theo thời gian", ""));
                    dlThang.SelectedIndex = 0;

                    int year1 = DateTime.Now.Year - 3;
                    int year2 = DateTime.Now.Year;
                    for (int i = year2; i >= year1; i--)
                    {
                        dlNam.Items.Add(new ListItem("Năm " + i.ToString(), i.ToString()));
                    }
                    dlNam.SelectedValue = year2.ToString();

                    txtTuNgay.Text = DateTime.Now.ToString("dd/MM/yyyy");
                    txtDenNgay.Text = DateTime.Now.ToString("dd/MM/yyyy");
                    dlNam.Visible = false;



                }
                else
                    gstGetMess("Bạn không có quyền xem trang này", "Home.aspx");

            }
        }
    }

    void LoadTK()
    {
        drtaikhoan.Items.Clear();
        drtaikhoan.DataSource = ex.GetData("sp_BaoCaoSoQuyNganHang_ListNganHang");
        drtaikhoan.DataBind();
        drtaikhoan.Items.Insert(0, new ListItem("--Chọn ngân hàng--", ""));
        drtaikhoan.SelectedIndex = 0;
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {

        LoadTK();
        Button1.Visible = true;
        Button2.Visible = true;
        Button3.Visible = true;
    }

    private void LoadFooter()
    {
        string[] field = { "sotien" };
        if (dt.Rows.Count > 0)
        {
            GVPhieuChi.FooterRow.Cells[2].Text = "Tổng cộng";
            GVPhieuChi.FooterRow.Cells[2].HorizontalAlign = HorizontalAlign.Center;
            for (int k = 3; k < 4; k++)
            {
                GVPhieuChi.FooterRow.Cells[k].Text = ConvertDecimal(double.Parse(dt.Compute("sum(" + field[k - 3] + ")", "").ToString()));
            }


        }
    }

    private void gstGetMess(string gstMess, string gstLink)
    {
        if (gstLink == "")
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), " ", "alert('" + gstMess + "')", true);
        else
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), " ", "alert('" + gstMess + "');window.location.href='" + gstLink + "'", true);

    }










    string ConvertDecimal(double s)
    {
        string q = "";
        if (s.ToString().Contains("."))
        {
            double x = double.Parse(s.ToString().Substring(0, s.ToString().IndexOf('.')));
            q = (x != 0 && x > 999) ? string.Format("{0:0,0}", x) : x.ToString();
            q += s.ToString().Substring(s.ToString().IndexOf('.'));
        }
        else
            q = (s != 0 && s > 999) ? string.Format("{0:0,0}", s) : s.ToString();
        return q;
    }


    void LoadFooterGVPhieuChi()
    {
        double sonhienlieu = 0;
        for (int i = 0; i < GVPhieuChi.Rows.Count; i++)
        {

            sonhienlieu += double.Parse(GVPhieuChi.Rows[i].Cells[3].Text.Replace(",", ""));


        }

        if (GVPhieuChi.Rows.Count > 0)
        {
            GVPhieuChi.FooterRow.Cells[1].Text = "Tổng số";
            GVPhieuChi.FooterRow.Cells[1].HorizontalAlign = HorizontalAlign.Center;
            GVPhieuChi.FooterRow.Cells[3].Text = sonhienlieu > 999 ? string.Format("{0:0,0}", sonhienlieu) : sonhienlieu.ToString();



        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        CurrentPage = 1;
        p = new SqlParameter[2];
        p[0] = new SqlParameter("@PageNumber", CurrentPage);
        p[0].SqlDbType = SqlDbType.Int;
        p[1] = new SqlParameter("@RowspPage", PageNumber);
        p[1].SqlDbType = SqlDbType.Int;

        GVPhieuChi.DataSource = ex.GetData("sp_BaoCaoPhieuChi_List", p);
        GVPhieuChi.DataBind();
        dt = ex.GetData("sp_BaoCaoPhieuChi_List_Footer");
        LoadFooter();
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        if (CurrentPage > 1)
        {
            CurrentPage--;


            p = new SqlParameter[2];
            p[0] = new SqlParameter("@PageNumber", CurrentPage);
            p[0].SqlDbType = SqlDbType.Int;
            p[1] = new SqlParameter("@RowspPage", PageNumber);
            p[1].SqlDbType = SqlDbType.Int;

            GVPhieuChi.DataSource = ex.GetData("sp_BaoCaoPhieuChi_List", p);
            GVPhieuChi.DataBind();
            dt = ex.GetData("sp_BaoCaoPhieuChi_List_Footer");
            LoadFooter();


        }
        else
        {
            Button1.Enabled = false;
            Button2.Enabled = false;
            Button3.Enabled = true;
        }
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        CurrentPage++;
        p = new SqlParameter[2];
        p[0] = new SqlParameter("@PageNumber", CurrentPage);
        p[0].SqlDbType = SqlDbType.Int;
        p[1] = new SqlParameter("@RowspPage", PageNumber);
        p[1].SqlDbType = SqlDbType.Int;

        GVPhieuChi.DataSource = ex.GetData("sp_BaoCaoPhieuChi_List", p);
        GVPhieuChi.DataBind();

        dt = ex.GetData("sp_BaoCaoPhieuChi_List_Footer");
        LoadFooter();
        if (GVPhieuChi.Rows.Count.Equals(0))
        {
            Button2.Enabled = true;
            Button3.Enabled = false;
        }
        else
        {
            Button2.Enabled = true;
            Button3.Enabled = true;
        }
        Button1.Enabled = true;
    }
    protected void btnXemPC_Click(object sender, EventArgs e)
    {
        getDate();
        if (drtaikhoan.SelectedIndex == 0)
        {
            gstGetMess("Chọn ngân hàng","");
        }
        else
        {
            p = new SqlParameter[3];
            p[0] = new SqlParameter("@TuNgay", ex.GetNgayThang(hdTuNgay.Value));
            p[0].SqlDbType = SqlDbType.SmallDateTime;
            p[1] = new SqlParameter("@DenNgay", ex.GetNgayThang(hdDenNgay.Value));
            p[1].SqlDbType = SqlDbType.SmallDateTime;


            p[2] = new SqlParameter("@NganHang", ex.GetGuid(drtaikhoan.SelectedValue));
            p[2].SqlDbType = SqlDbType.UniqueIdentifier;
            GVPhieuChi.DataSource = ex.GetData("sp_BaoCaoSoQuyNganHang_TheoThoiGian_NganHang", p);
            GVPhieuChi.DataBind();
            double tiencuoiki;
            for (int i = 0; i < GVPhieuChi.Rows.Count - 1; i++)
            {
                if (GVPhieuChi.Rows[i].Cells[0].Text.Equals(GVPhieuChi.Rows[i + 1].Cells[0].Text))
                {

                    GVPhieuChi.Rows[i + 1].Cells[8].Text = GVPhieuChi.Rows[i].Cells[9].Text;
                    tiencuoiki = double.Parse(GVPhieuChi.Rows[i + 1].Cells[6].Text) - double.Parse(GVPhieuChi.Rows[i + 1].Cells[7].Text) + double.Parse(GVPhieuChi.Rows[i + 1].Cells[8].Text);
                    GVPhieuChi.Rows[i + 1].Cells[9].Text = tiencuoiki == 0 ? "0" : string.Format("{0:0,0}", tiencuoiki);
                }
            }
            GVPhieuChi.Visible = true;

            Button2.Visible = false;
            Button3.Visible = false;
            Button1.Visible = false;

        }

        //Button1.Visible = false;
        //Button2.Visible = false;
        //Button3.Visible = false;

        //p = new SqlParameter[2];
        //p[0] = new SqlParameter("@TuNgay", ex.GetNgayThang(hdTuNgay.Value));
        //p[0].SqlDbType = SqlDbType.SmallDateTime;
        //p[1] = new SqlParameter("@DenNgay", ex.GetNgayThang(hdDenNgay.Value));
        //p[1].SqlDbType = SqlDbType.SmallDateTime;

        //dtPrint = ex.GetData("sp_BaoCaoPhieuChi_List_TheoThoiGian_PrintThoTK", p);
    }
    public void getDate()
    {
        if (dlThang.SelectedIndex.Equals(0))
        {
            hdTuNgay.Value = txtTuNgay.Text;
            hdDenNgay.Value = txtDenNgay.Text;
        }
        else
        {
            txtTuNgay.Text = "1/" + dlThang.SelectedValue + "/" + dlNam.SelectedValue;
            txtDenNgay.Text = GetLastDayOfMonth(int.Parse(dlThang.SelectedValue)).ToString("dd/MM/yyyy");
            hdTuNgay.Value = txtTuNgay.Text;
            hdDenNgay.Value = txtDenNgay.Text;
        }
    }
    public DateTime GetLastDayOfMonth(int iMonth)
    {
        DateTime dtResult = new DateTime(int.Parse(dlNam.SelectedValue), iMonth, 1);
        dtResult = dtResult.AddMonths(1);
        dtResult = dtResult.AddDays(-(dtResult.Day));
        return dtResult;
    }


    static public void releaseObject(object obj)
    {
        try
        {
            System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
            obj = null;
        }
        catch (Exception ex)
        {
            obj = null;
            throw new Exception("Exception Occured while releasing object " + ex.ToString());
        }
        finally
        {
            GC.Collect();
        }
    }

    protected void dlThang_SelectedIndexChanged(object sender, EventArgs e)
    {


        if (dlThang.SelectedIndex.Equals(0))
        {
            txtTuNgay.Visible = true;
            txtDenNgay.Visible = true;
            dlNam.Visible = false;
        }
        else
        {
            txtTuNgay.Visible = false;
            txtDenNgay.Visible = false;
            dlNam.Visible = true;
        }
    }
}